<%--
  Created by IntelliJ IDEA.
  User: SLeeP
  Date: 28.04.12
  Time: 23:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="db.*" %>

<html>
  <head>
      <title>Login in WebBanking</title>
      <style type="text/css">
          body {
              font-family: verdana, tahoma, sans-serif;
              color: #333366;
              text-align: center;
              margin-top: 50px;
          }
      </style>
      <%
          DBConnection dbConnection = new DBConnection();
          String login = request.getParameter("login");
          String password = request.getParameter("password");
          int user_id = -1;

          if (login != null && password != null) {
              if ((user_id = dbConnection.login(login, password)) != -1) {
                  if (dbConnection.getRole(user_id)== DBConnection.Role.CLIENT)
                  %> <meta http-equiv="Refresh" content="0; url=accounts.jsp">
      <%
                  if (dbConnection.getRole(user_id)== DBConnection.Role.MODERATOR)
                  %> <meta http-equiv="Refresh" content="0; url=moderator/messages.jsp">
      <%
                  if (dbConnection.getRole(user_id)== DBConnection.Role.ADMIN) {%>
                        <meta http-equiv="Refresh" content="0; url=admin/operations.jsp">
                  <% }
                  Cookie cookie = new Cookie("user_id", user_id + "");
                  String secret = dbConnection.getRandomString();
                  Cookie cookie2 = new Cookie("secret", secret);
                  response.addCookie(cookie);
                  response.addCookie(cookie2);

                  dbConnection.addCookie(user_id, secret);
              } else {
                  user_id = 0;
              }
          }

      %>
      <script type="text/javascript"> document.cookie = "";</script>
  </head>
  <body>
  <h3> Login in WebBanking System</h3>
  <form method="post" action="index.jsp">
      <table align="center">
          <tr>
              <td> Login: </td>
              <td> <input type=text name="login" id = "login" size=16> </td>
              <td> </td>
          </tr>
          <tr>
              <td> Password: </td>
              <td><input type=password name=password size=16> </td>
              <td style="color: red"> <% if (user_id == 0) {
                  %>Wrong password<%
              }%> </td>
          </tr>
          <tr align="center">
              <td><input type="submit" value="login"></td>
          </tr>
      </table>
  </form>

  </body>
</html>